import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Unique,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';

import { User, DietFood } from '@/modules/app/entities';
import { ApiHideProperty } from '@nestjs/swagger';
import { IsEnum, IsNumber } from 'class-validator';

export enum MealType {
  /**
   * 早餐
   */
  BREAKFAST = 'BREAKFAST',
  /**
   * 午餐
   */
  LUNCH = 'LUNCH',
  /**
   * 晚餐
   */
  DINNER = 'DINNER',
  /**
   * 加餐
   */
  SNACK = 'SNACK',
}

@Entity('diet_logs')
// 只会在新建表或新建索引时，根据你实体的字段生成唯一索引，如果更换这个，同步就会报错
// 他是删除原有索引再去加新的索引，但是又无法删除，导致报错，需要手动去删除外键
@Unique(['userId', 'mealType', 'logDate'])
export class DietLog {
  @PrimaryGeneratedColumn('increment')
  dietLogId: number;

  @IsNumber()
  @Column()
  userId: number;

  @ApiHideProperty()
  @ManyToOne(() => User, (user) => user.dietLogs)
  @JoinColumn({ name: 'userId' })
  user: User;

  /**
   * 进餐类型
   */
  @IsEnum(MealType)
  @Column({
    type: 'enum',
    enum: MealType,
  })
  mealType: MealType;

  // 多对多，多个dietLog可以有多个food
  @OneToMany(() => DietFood, (dietFood) => dietFood.dietLog)
  dietFoods: DietFood[];

  /**
   * 日期
   * @example 2025-10-01
   */
  @Column()
  logDate?: String = new Date().toISOString().slice(0, 10);

  @CreateDateColumn()
  createdAt?: Date;

  @UpdateDateColumn()
  updatedAt?: Date;
}
